$(document).ready(function() {

    $( "#rent_slider" ).slider({
        range: "min",
        value: 800,
        min: 500,
        max: 1000,
        step: 10,
        slide: function( event, ui ) {
            $( "#rent_amount" ).val( ui.value );
            $("#sb_lf").text(parseInt($("#rent_amount").val()) + parseInt($("#food_amount").val()) + parseInt($("#transport_amount").val()));
        }
    });
    $( "#rent_amount" ).val( $( "#rent_slider" ).slider( "value" ) );

    $( "#food_slider" ).slider({
        range: "min",
        value: 200,
        min: 100,
        max: 300,
        step: 10,
        slide: function( event, ui ) {
            $( "#food_amount" ).val( ui.value );
            $("#sb_lf").text(parseInt($("#rent_amount").val()) + parseInt($("#food_amount").val()) + parseInt($("#transport_amount").val()));
        }
    });
    $( "#food_amount" ).val( $( "#food_slider" ).slider( "value" ) );

    $( "#transport_slider" ).slider({
        range: "min",
        value: 400,
        min: 300,
        max: 700,
        step: 10,
        slide: function( event, ui ) {
            $( "#transport_amount" ).val( ui.value );
            $("#sb_lf").text(parseInt($("#rent_amount").val()) + parseInt($("#food_amount").val()) + parseInt($("#transport_amount").val()));
        }
    });
    
    $( "#transport_amount" ).val( $( "#transport_slider" ).slider( "value" ) );

    $( "#lifestyle" ).val ( $("#get_lifestyle").val());

    $(".life_expenses").change(function () {
        var lifestyle = $("#lifestyle option:selected").val();
        var country = $("#location option:selected").val();
        var urls = $("#link_get_monthly_ex").text();
        $.post(urls, {
            'lifestyle':lifestyle,
            'country':country
        }, function(data){
            var values = data.split("|");
            var minimumRent = values[0];
            var maximumRent = values[1];
            var recommendedRent = values[2];

            var minimumFoodCost = values[3];
            var maximumFoodCost = values[4];
            var recommendedFoodCost = values[5];

            var minimumTransport = values[6];
            var maximumTransport = values[7];
            var recommendedTransport = values[8];

            if($("#lifestyle option:selected").val() == $("#get_lifestyle").val()){
                recommendedRent = $("#get_rent_selected").val();
                recommendedFoodCost = $("#get_food_selected").val();
                recommendedTransport = $("#get_transport_selected").val();
            }

            var rent_id = values[9];
            var food_id = values[10];
            var transport_id = values[11];
            
            $("#rent_id").val(rent_id);
            $("#rent_slider").slider( "option", "min", parseInt(minimumRent));
            $("#rent_slider").slider( "option", "max", parseInt(maximumRent));
            $("#rent_slider").slider( "option", "value", parseInt(recommendedRent));
            $( "#rent_amount" ).val( $( "#rent_slider" ).slider( "value" ) );

            $("#food_id").val(food_id);
            $("#food_slider").slider( "option", "min", parseInt(minimumFoodCost));
            $("#food_slider").slider( "option", "max", parseInt(maximumFoodCost));
            $("#food_slider").slider( "option", "value", parseInt(recommendedFoodCost));
            $( "#food_amount" ).val( $( "#food_slider" ).slider( "value" ) );

            $("#transport_id").val(transport_id);
            $("#transport_slider").slider( "option", "min", parseInt(minimumTransport));
            $("#transport_slider").slider( "option", "max", parseInt(maximumTransport));
            $("#transport_slider").slider( "option", "value", parseInt(recommendedTransport));
            $( "#transport_amount" ).val( $( "#transport_slider" ).slider( "value" ) );
			
			$("#sb_lf").text(parseInt($("#rent_amount").val()) + parseInt($("#food_amount").val()) + parseInt($("#transport_amount").val()));
        });
    })
    .change();

    $("#fly_times").val($("#get_travel_times").val());
    $("[name=ticket_type]").filter("[value="+$("#get_ticket_type").val()+"]").attr("checked","checked");
    //$('input[name=ticket_type]:checked').val($("#get_ticket_type").val());

    $(".travel_cost").change(function () {
        var country = $("#location option:selected").val();
        var ticketClass = $('input[name=ticket_type]:checked').val();
        var fly_times = $("#fly_times option:selected").val();
        var urls = $("#link_get_ticket_cost").text();
        $.post(urls, {
            'country':country,
            'ticketClass':ticketClass,
            'fly_times':fly_times
        }, function(data){
            var values = data.split("|");
            $("#ticket_cost").text(values[0]);
            $("#travel_id").val(values[1]);
        });
    })
    .change();

    $("#location").val($("#get_country").val());
    $("#sch_type").val($("#get_sch_type").val());
    $("#course").val($("#get_course").val());

    $(".sch_fee").change(function () {
        var country = $("#location option:selected").val();
        var inst_type = $("#sch_type option:selected").val();
        var course = $("#course option:selected").val();
        var urls = $("#link_get_sch_fee").text();
        $.post(urls, {
            'country':country,
            'inst_type':inst_type,
            'course':course
        }, function(data){
            $("#tuition_fees").text(data);
        });
    })
    .change();

    $("#currency").change(function () {
        if($("#currency option:selected").val() == "yuan"){
            $("#converter").text(($("#grand_total").text()*6.5) + " Chinese Yuan");
        }else if($("#currency option:selected").val() == "dong"){
            $("#converter").text("VND " + ($("#grand_total").text()*20570));
        }else if($("#currency option:selected").val() == "rupiah"){
            $("#converter").text("IDR " + ($("#grand_total").text()*8547));
        }
    })
    .change();

    $(".total_amount").change(function () {
        var total_expenses = $("#rent_amount").val()
            + $("#food_amount").val()
            + $("#transport_amount").val();
        var total_ticketCost = $("#ticket_cost").val();
        var total_tuitionFees = $("#tuition_fees").val();
        var grand_total = total_expenses + total_ticketCost + total_tuitionFees;
        //$("#grand_total").text($("#rent_amount").val());
        //alert($("#ticket_cost").text());
    })
    .change();

    $(".grand_total_values").change(function () {
        var grand_total = $("#sb_lf").text();
        $("#grand_total").text(grand_total);
    })
    .change();
    
    $("#calculate").click(function () {
        cal_total();
    });
	
	function cal_total(){
		var total = parseInt($("#sb_lf").text())*12 + parseInt($("#ticket_cost").text()) + parseInt($("#tuition_fees").text())*2;
        $("#grand_total").text(total);
        if($("#currency option:selected").val() == "yuan"){
            $("#converter").text(($("#grand_total").text()*6.5) + " Chinese Yuan");
        }else if($("#currency option:selected").val() == "dong"){
            $("#converter").text("VND " + ($("#grand_total").text()*20570));
        }else if($("#currency option:selected").val() == "rupiah"){
            $("#converter").text("IDR " + ($("#grand_total").text()*8547));
        }
	}

});